package com.zhkj.txg.module.mine.ui

import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.zhkj.lib.base.BaseActivity
import com.zhkj.txg.R
import kotlinx.android.synthetic.main.activity_coupon_center.*

class CouponCenterActivity : BaseActivity() {
    override val layoutResId: Int = R.layout.activity_coupon_center
    override fun statusTitleBar(): View? = titleCouponCenter

    private val allFragment: CouponCenterFragment by lazy {
        CouponCenterFragment.newInstance(CouponCenterFragment.TYPE_ALL)
    }
    private val typeAllFragment: CouponCenterFragment by lazy {
        CouponCenterFragment.newInstance(CouponCenterFragment.TYPE_ALL_AREA)
    }
    private val typeShopFragment: CouponCenterFragment by lazy {
        CouponCenterFragment.newInstance(CouponCenterFragment.TYPE_SHOP)
    }
    private val typeSingleFragment: CouponCenterFragment by lazy {
        CouponCenterFragment.newInstance(CouponCenterFragment.TYPE_GOODS)
    }

    override fun initUI() {
        with(vpCouponCenter) {
            adapter = object : FragmentStateAdapter(this@CouponCenterActivity) {
                override fun getItemCount(): Int = 4
                override fun createFragment(position: Int): Fragment = getCurrentFragment(position)
            }
            getChildAt(0)?.overScrollMode = View.OVER_SCROLL_NEVER
        }
        TabLayoutMediator(
            tabCouponCenter, vpCouponCenter,
            TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                when (position) {
                    1 -> tab.customView =
                        layoutInflater.inflate(R.layout.item_collect_shop, null).apply {
                            findViewById<TextView>(R.id.tvTab).text =
                                getString(R.string.coupon_type_all)
                        }
                    2 -> tab.customView =
                        layoutInflater.inflate(R.layout.item_collect_shop, null).apply {
                            findViewById<TextView>(R.id.tvTab).text =
                                getString(R.string.coupon_type_shops)
                        }
                    3 -> tab.customView =
                        layoutInflater.inflate(R.layout.item_collect_shop, null).apply {
                            findViewById<TextView>(R.id.tvTab).text =
                                getString(R.string.coupon_type_single)
                        }
                    else -> tab.customView =
                        layoutInflater.inflate(R.layout.item_collect_shop, null).apply {
                            findViewById<TextView>(R.id.tvTab).text = getString(R.string.all)
                        }
                }
            }).attach()
    }

    private fun getCurrentFragment(position: Int): CouponCenterFragment = when (position) {
        1 -> typeAllFragment
        2 -> typeShopFragment
        3 -> typeSingleFragment
        else -> allFragment
    }
}